package sdk.webview.fmc.com.fmcsdk.h5;

import android.content.Context;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import sdk.webview.fmc.com.fmcsdk.util.XLog;

/**
 * Created by ydm on 2017/7/4.
 */

public class H5WebViewClient extends WebViewClient {

    private WebView webView;
    /**加载失败*/
    private OnReceivedErrorListener errorListener;
    private Context context;
    public H5WebViewClient(WebView webView, OnReceivedErrorListener errorListener , Context context){
        this.webView = webView;
        this.context = context;
        this.errorListener = errorListener;
    }

    @Override
    public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // 这里可自定义页面打开url，否则会调用系统自带浏览器打开url
//        if (webView != null){
//            webView.loadUrl(url);
//            return true;
//        }
        XLog.i("shouldOverrideUrlLoading=="+url);
        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //页面加载完成
        if (webView != null){
        }
        super.onPageFinished(view, url);
        //加载完成回调监听
        if (errorListener!=null) {
            errorListener.onreceivedSuccess(url);
        }


    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        //页面加载失败
        if (errorListener != null){
            errorListener.onReceivedError(view, errorCode, description, failingUrl);
        }
        XLog.i(errorCode+"=="+description+"=="+failingUrl);
    }

    @Override
    public void onScaleChanged(WebView webView, float v, float v1) {
        super.onScaleChanged(webView, v, v1);
    }

    /**
     * 设置加载失败的接口
     * @param errorListener
     */
    public void setReceivedErrorListener(OnReceivedErrorListener errorListener){
        this.errorListener = errorListener;
    }
    //加载失败的接口
    public interface OnReceivedErrorListener{
        void onReceivedError(WebView view, int errorCode, String description, String failingUrl);
        void onreceivedSuccess(String url);
    }

}
